package mailclient.util;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.JWindow;

public class WarnFrame extends JWindow implements Runnable, MouseListener {

	private static final long serialVersionUID = -11L;
	private Integer screenWidth; // 屏幕宽度
	private Integer screenHeight; // 屏幕高度
	private Integer windowWidth; // 设置提示窗口宽度
	private Integer windowHeight; // 设置提示窗口高度
	private Integer bottmToolKitHeight; // 底部任务栏高度，如果没有任务栏则为零
	private Integer stayTime; // 提示框停留时间

	private Integer x; // 窗口起始X坐标
	private Integer y; // 窗口起始Y坐标

	private String title;
	private String message;

	private JPanel mainPanel; // 主面板
	private JLabel titleLabel; // 标题栏标签
	private JPanel titlePanel; // 标题栏面板
	private JTextPane textPane1; // 内容标签
	private JPanel messagePanel; // 内容面板

	public static void main(String[] args) {
		Runnable warnFrame = new WarnFrame(
				"");
		// Runnable warnFrame=new
		// WarnFrame(" 达州一个平凡的家庭明尼苏达州一个平凡的家庭明尼苏达州一个平凡的家");
		Thread thread = new Thread(warnFrame);
		thread.start();
	}

	public WarnFrame(String message) {
		// new WarnFrame("400","250","15","消息提示",message);
		this.windowWidth = 200;
		this.windowHeight = 50;
		this.stayTime = 30 * 1000;
		this.title = "您有新邮件(点击右键关闭)";
		this.message = message;
		this.init();
	}

	public WarnFrame(String width, String height, String sleeptime, String title, String message) {
		this.windowWidth = Integer.parseInt(width);
		this.windowHeight = Integer.parseInt(height);
		this.stayTime = Integer.parseInt(sleeptime) * 1000;
		this.title = title;
		this.message = message;
		this.init();
	}

	private void init() {
		bottmToolKitHeight = Toolkit.getDefaultToolkit().getScreenInsets(this.getGraphicsConfiguration()).bottom;
		Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize();
		screenWidth = dimension.width;
		screenHeight = dimension.height;

		x = screenWidth - windowWidth;
		y = screenHeight;
		this.setLocation(x, y - bottmToolKitHeight - windowHeight);
		mainPanel = new JPanel(new BorderLayout());

		titleLabel = new JLabel(title);
		titleLabel.setFont(new Font("微软雅黑", Font.BOLD, 15));
		titleLabel.setForeground(Color.WHITE);
		titlePanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
		titlePanel.setBackground(Color.GRAY) ;
		titlePanel.add(titleLabel);

		textPane1 = new JTextPane();
		textPane1.setBackground(Color.WHITE);
		textPane1.setText(message);
		textPane1.setAutoscrolls(true);
		textPane1.setPreferredSize(new Dimension(370, 210));
		textPane1.setFont(new Font("微软雅黑", Font.PLAIN, 15));

		messagePanel = new JPanel();
		messagePanel.add(textPane1);
		messagePanel.setBackground(Color.GRAY);
		JScrollPane sp = new JScrollPane(messagePanel);

		mainPanel.add(titlePanel, BorderLayout.NORTH);
		// mainPanel.add(messagePanel, BorderLayout.CENTER);
		mainPanel.add(sp, BorderLayout.CENTER);

		this.setSize(windowWidth, windowHeight);
		
		this.getContentPane().add(mainPanel);
		this.setAlwaysOnTop(false);
		this.addMouseListener(this);
		Toolkit.getDefaultToolkit().beep();
		this.setVisible(true);
	}

	public void run() {
		Integer delay = 1;// 出现和消失的速度
		Integer step = 1;
		Integer end = windowHeight + bottmToolKitHeight;
		while (true) {
			try {
				step++;
				y = y - 1;
				this.setLocation(x, y);
				if (step > end) {
					Thread.sleep(stayTime);
					break;
				}
				Thread.sleep(delay);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		
		step = 1;
		while (true) {
			try {
				step++;
				y = y + 1;
				this.setLocation(x, y);
				if (step > end) {
					this.dispose();
					break;
				}
				Thread.sleep(delay);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}

	public void mouseClicked(MouseEvent e) {
		if (e.getButton() == 3) {
			this.dispose();
		}
	}

	public void mouseEntered(MouseEvent e) {
	}

	public void mouseExited(MouseEvent e) {
	}

	public void mousePressed(MouseEvent e) {
	}

	public void mouseReleased(MouseEvent e) {
	}

}